//
//  CTFinanceManageVC.m
//  digitalCurrency
//
//  Created by apple on 2019/6/12.
//  Copyright © 2019 XinHuoKeJi. All rights reserved.
//

#import "CTFinanceManageVC.h"
#import "TotalFinanceHeadView.h"
#import "HomeNetManager.h"
#import "CTQGCell.h"

@interface CTFinanceManageVC ()

@property (nonatomic,copy) NSMutableArray *contentArr;
@property (nonatomic, strong) LYEmptyView *emptyView;
@property (nonatomic, strong) TotalFinanceHeadView *homeHeadView;

@property (nonatomic, strong) UIButton *transformIn;
@property (nonatomic, strong) UIButton *transformOut;

@property (nonatomic, assign) NSInteger page;
@end

@implementation CTFinanceManageVC

- (void)viewDidLoad {
    [super viewDidLoad];
    self.viewTitle.text = LocalizationKey(@"Revenue Finance");
    _page = 1;
    [self.view addSubview:self.tableView];
    self.tableView.rowHeight = 60;
    self.tableView.separatorColor = [UIColor clearColor];
    [self.tableView registerClass:[CTQGCell class] forCellReuseIdentifier:@"qgcell"];
    
    [self setTablewViewHeard];
    [self headRefreshWithScrollerView:self.tableView];
    [self footRefreshWithScrollerView:self.tableView];
    
    self.tableView.ly_emptyView = self.emptyView;
    
    [self.view addSubview:self.transformIn];
    [self.view addSubview:self.transformOut];
}

- (void)viewWillLayoutSubviews {
    [super viewWillLayoutSubviews];
    CGRect rect = self.view.bounds;
    self.tableView.frame = rect;
}

#pragma mark-下拉刷新数据
- (void)refreshHeaderAction {
    _page = 1;
    [self getFinanceList];
    [self getFinancingsInfo];
}

- (void)refreshFooterAction {
    _page++;
    [self getFinanceList];
}

-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:YES];
    
    CGRect rect = self.view.bounds;
    rect.size.height -= (kTabBarHeight+20);
    self.tableView.frame = rect;
    
    if(![YLUserInfo isLogIn]) {
        [self showLoginViewController];
    }
    else {
        [self getFinancingsInfo];
        [self getFinanceList];
    }
}

-(void)setTablewViewHeard {
    self.homeHeadView = [[TotalFinanceHeadView alloc]initWithFrame:CGRectMake(0, 0, kWindowW,  123)];
    self.tableView.tableHeaderView = self.homeHeadView;
    self.tableView.tableFooterView = [UIView new];
    
    if (@available(iOS 11.0, *)){
        self.tableView.estimatedRowHeight = 0;
        self.tableView.estimatedSectionHeaderHeight = 0;
        self.tableView.estimatedSectionFooterHeight = 0;
    }
}

- (void)getFinanceList {
    __weak typeof(self)weakself = self;
    [EasyShowLodingView showLodingText:LocalizationKey(@"loading")];
    NSDictionary *dic = @{@"pageNum":@(_page),@"pageSize":@"20"};
    [HomeNetManager getAllFinancingsIncomeLogsWithInfo:dic CompleteHandle:^(id resPonseObj, int code) {
        [EasyShowLodingView hidenLoding];
        NSLog(@"resPonseObj:%@",resPonseObj);
        if (code) {
            NSInteger codeInt = [resPonseObj[@"code"] integerValue];
            if (codeInt == 0) {
                NSArray *data = resPonseObj[@"data"];
                
                if (_page == 1) {
                    [self.contentArr removeAllObjects];
                }
                else {
                    if (data.count == 0) {
                        self.tableView.mj_footer.state = MJRefreshStateNoMoreData;
                    }
                }
                
                for (NSDictionary *sub in data) {
                    [self.contentArr addObject:sub];
                }
                self.tableView.ly_emptyView = self.emptyView;
                [self.tableView reloadData];
            }
            else if (codeInt == -1000001 ){
                [ShowLoGinVC showLoginVc:self withTipMessage:resPonseObj[MESSAGE]];
            }
            else {
                [weakself.view makeToast:resPonseObj[MESSAGE] duration:1.5 position:CSToastPositionCenter];
            }
        }
        else {
            [weakself.view makeToast:[[ChangeLanguage bundle] localizedStringForKey:@"noNetworkStatus" value:nil table:@"English"] duration:1.5 position:CSToastPositionCenter];
        }
    }];
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.contentArr.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    CTQGCell *cell = [CTQGCell createTableView:tableView];
    
    NSDictionary *dic = _contentArr[indexPath.row];
    cell.createTime.text = [NSString stringWithFormat:@"%@",dic[@"createTime"]];
    cell.coinNumber.text = [NSString stringWithFormat:@"%@", [ToolUtil judgeStringForDecimalPlaces:[NSString stringWithFormat:@"%@",dic[@"amount"]] point:4]];
    cell.coinName.text = @"DWTT";
    
    cell.bgView.frame = CGRectMake(0, 0, kWindowW, 62);
    cell.bgView.layer.cornerRadius = 0.01;
    cell.bgView.layer.shadowColor = [UIColor whiteColor].CGColor;
    
    if (indexPath.row%2!=0) {
        cell.bgView.backgroundColor = [[UIColor colorWithHEX:mainBlackColor] colorWithAlphaComponent:0.5];
    }
    else {
        cell.bgView.backgroundColor = [UIColor whiteColor];
    }
    
    return cell;
}

- (void)getFinancingsInfo {
    [HomeNetManager getAllFinancingsInfo:@{@"userId":[YLUserInfo shareUserInfo].ID} CompleteHandle:^(id resPonseObj, int code) {
        if (code) {
            [self.contentArr removeAllObjects];

            NSDictionary *data = resPonseObj[@"data"];
           
            NSString *Asset = [data[@"allFinancingsAsset"] stringValue];
            self.homeHeadView.amountCoin.text = [NSString stringWithFormat:@"%@",Asset];
            
            NSString *Income = [data[@"allFinancingsIncome"] stringValue];
            self.homeHeadView.canUsedCoin.text = [NSString stringWithFormat:@"%@",Income];

        }
        else {
            [self.view makeToast:LocalizationKey(@"noNetworkStatus") duration:1.5 position:CSToastPositionCenter];
        }
    }];
}

- (LYEmptyView *)emptyView {
    if (!_emptyView) {
        _emptyView = [LYEmptyView emptyViewWithImageStr:@"emptyData" titleStr:[[ChangeLanguage bundle] localizedStringForKey:@"NoData" value:nil table:@"English"]];
        _emptyView.titleLabTextColor = tThreeColor;
        _emptyView.titleLabFont = [UIFont systemFontOfSize:19];
    }
    return _emptyView;
}


- (NSMutableArray *)contentArr
{
    if (!_contentArr) {
        _contentArr = [NSMutableArray array];
    }
    return _contentArr;
}

@end
